package com.asa.ui.J.A;

import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Arrays;

import com.asa.ui.frame.AlgoVisHelper;


public class AlgoVisualizer {
	private AlgoFrame frame;

	private FractalData data;
	
	public AlgoVisualizer(int maxDepth){

		final int screenWidth = (int) Math.pow(3, maxDepth);
		final int screenHeight = (int) Math.pow(3, maxDepth);

		
		data = new FractalData(maxDepth);
		
		EventQueue.invokeLater(new Runnable() {//用java事件分发（开了个线程）的方法来创建对话框
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				frame=new AlgoFrame("分型几何",screenWidth,screenHeight);
				frame.addKeyListener(new AlgoKeyListener());
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						runmiage();
					}
	
				}).start(); ;
				
			}
		});

	}
	
	
	
	private void runmiage() {
		// TODO Auto-generated method stub
		setData(1);
	}



	private void setData(int depth) {
		// TODO Auto-generated method stub
		
		if (depth>=0) {
			data.setDepth(depth);
		}
		
		frame.render(data);
		AlgoVisHelper.pause(100);
		
	}
	
	
	public class AlgoKeyListener extends KeyAdapter{
		
		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			super.keyReleased(e);
			if (e.getKeyChar()>='0'&&e.getKeyChar()<='9') {
				
				int depth = e.getKeyChar() - '0';
				
				setData(depth);
				
			}
			
			
		}
		
	}
	
	
	
	
	

}